<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>SqlMan/Query Concept</title>
    <meta name="description" content="TODO"/>
    <meta name="keywords" content="SqlMan, document, java sql, java jdbc, java orm, jdbc tools, jdbc helper, last one"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css"/>
    <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
    <link rel="icon" type="image/png" href="/asset/favicon.png"/>
    <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
    <script>
            // 获取用户的默认语言
            var userLang = navigator.language || navigator.userLanguage;

            // 检查是否为中文环境（包括简体和繁体）
            if (userLang.startsWith('zh') && location.pathname.indexOf('cn') == -1) {
                 confirm('欢迎！您可以改为访问中文内容。是否继续？') && location.assign('/docs/cn');  // 如果是中文，则弹出提示
            }

            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?3cb62106ad945fc91efddfe250e1542e";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();

    </script>
</head>
<body>
<nav>
    <div>
        <div class="links">
            <a href="/">🏠 Home</a>
            |
            ⚙️ Source:
            <a target="_blank" href="https://github.com/lightweight-component/SqlMan">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/SqlMan">Gitcode</a>

            | <a href="/docs/cn">Chinese Version</a>
        </div>
        <h1>
            <img src="/asset/favicon-2x.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;"/> SqlMan
        </h1>
        <h3>Lightweight Data Access Tools -- User Manual
        </h3>
    </div>
</nav>
<div>
    <menu>

        <ul>
            <li class="selected">
                <a href="/docs">Home</a>
            </li>
            <li>
                <a href="/docs/quick-start">Quick Start</a>
            </li>
            <li>
                <a href="/docs/setup">Setup</a>
            </li>
        </ul>
        <h3>Querying the Database</h3>
        <ul>
            <li>
                <a href="/docs/query/query-concept">Query Concept</a>
            </li>
            <li>
                <a href="/docs/query/query-tutorial">Query Tutorial</a>
            </li>
            <li>
                <a href="/docs/query/query-paging">Paging Query</a>
            </li>
        </ul>
        <h3>Writing the data</h3>
        <ul>
            <li>
                <a href="/docs/write/Create-Update-Delete">Create/Update/Delete</a></li>
            <li>
                <a href="/docs/write/batch">Batch Update</a>
            </li>
        </ul>
        <h3>Other Usage</h3>
        <ul>
            <li>
                <a href="/docs/other/sql-xml">SQL in XML</a></li>
            <li>
                <a href="/docs/other/entity">No SQL but Entity</a>
            </li>
        </ul>
        <h3>Misc</h3>
        <ul>
            <li><a href="/docs/common/versions">Release History</a></li>
            <li><a href="/docs/common/contact">Contact</a></li>
        </ul>
    </menu>
    <article class="aj-text">
        <h1>Batch Update</h1>
        <p>The BatchUpdate class in SqlMan provides efficient methods for performing batch operations on database records, particularly useful when you need to insert or update multiple records simultaneously.</p>
        <h2>Basic Batch Insert Operations</h2>
        <h3>Method 1: Simple Batch Insert</h3>
        <p>For basic batch inserts with raw field values:</p>
        <pre><code class="language-java">BatchUpdate batchUpdate = new BatchUpdate();
batchUpdate.setTableName(&quot;users&quot;);

// Prepare your data
String fields = &quot;name, email, age&quot;;
List&lt;String&gt; values = Arrays.asList(
    &quot;('John', 'john@email.com', 25)&quot;,
    &quot;('Jane', 'jane@email.com', 30)&quot;
);

// Execute batch insert
batchUpdate.createBatch(fields, values);
</code></pre>
        <h3>Method 2: Batch Insert with Maps</h3>
        <p>For inserting multiple records using Map objects:</p>
        <pre><code class="language-java">List&lt;Map&lt;String, Object&gt;&gt; users = new ArrayList&lt;&gt;();

Map&lt;String, Object&gt; user1 = new HashMap&lt;&gt;();
user1.put(&quot;name&quot;, &quot;John&quot;);
user1.put(&quot;email&quot;, &quot;john@email.com&quot;);
users.add(user1);

Map&lt;String, Object&gt; user2 = new HashMap&lt;&gt;();
user2.put(&quot;name&quot;, &quot;Jane&quot;);
user2.put(&quot;email&quot;, &quot;jane@email.com&quot;);
users.add(user2);

batchUpdate.createBatchMap(users, &quot;users&quot;);
</code></pre>
        <h3>Method 3: Batch Insert with Java Beans</h3>
        <p>For inserting multiple records using Java objects:</p>
        <pre><code class="language-java">List&lt;User&gt; users = new ArrayList&lt;&gt;();
User user1 = new User();
user1.setName(&quot;John&quot;);
user1.setEmail(&quot;john@email.com&quot;);
users.add(user1);

User user2 = new User();
user2.setName(&quot;Jane&quot;);
user2.setEmail(&quot;jane@email.com&quot;);
users.add(user2);

batchUpdate.setTableName(&quot;users&quot;);
batchUpdate.createBatch(users);
</code></pre>
        <h2>Batch Delete Operations</h2>
        <p>To delete multiple records by their IDs:</p>
        <pre><code class="language-java">BatchUpdate batchUpdate = new BatchUpdate();
batchUpdate.setTableName(&quot;users&quot;);
batchUpdate.setIdField(&quot;id&quot;);

List&lt;Integer&gt; idsToDelete = Arrays.asList(1, 2, 3, 4);
UpdateResult result = batchUpdate.deleteBatch(idsToDelete);

if (result.isOk()) {
    System.out.println(&quot;Batch delete successful&quot;);
}
</code></pre>
        <h2>Best Practices</h2>
        <ol>
            <li>
                <p><strong>Transaction Management</strong></p>
                <ul>
                    <li>The class handles transactions automatically</li>
                    <li>Auto-commit is disabled during batch operations</li>
                    <li>Rollback is performed automatically on failure</li>
                </ul>
            </li>
            <li>
                <p><strong>Data Type Handling</strong>
                    The class automatically handles various data types:</p>
                <ul>
                    <li>Strings are properly quoted</li>
                    <li>Booleans are converted to 1/0</li>
                    <li>Dates are formatted appropriately</li>
                    <li>LocalDateTime is supported</li>
                </ul>
            </li>
            <li>
                <p><strong>Performance Tips</strong></p>
                <ul>
                    <li>Use batch operations instead of individual inserts for better performance</li>
                    <li>Keep batch sizes reasonable (typically 100-1000 records)</li>
                    <li>Monitor memory usage when dealing with large datasets</li>
                </ul>
            </li>
            <li>
                <p><strong>Error Handling</strong></p>
                <ul>
                    <li>Operations are logged</li>
                    <li>Transactions are rolled back on failure</li>
                    <li>Execution results are available for verification</li>
                </ul>
            </li>
        </ol>
        <p>This BatchUpdate class significantly improves performance when dealing with multiple database operations by reducing the number of database round-trips.</p>

    </article>
</div>
<footer>
    SqlMan, a part of <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> open source. Mail:
    frank@ajaxjs.com, visit <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>.
    <br/>
    <br/>
    Copyright © 2025 Frank Cheung. All rights reserved.
    <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?3cb62106ad945fc91efddfe250e1542e";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();

    </script>
</footer>
</body>
</html>